<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script>
        window.onload = function() {
            var funcs1 = [];
            for (var i = 0; i < 10; i++) {
                funcs1.push(function() {
                    console.log(i);
                });
            }
            funcs1.forEach(function(func) {
                    func();
                })
                // IIFE
            var funcs2 = [];
            for (var i = 0; i < 10; i++) {
                funcs2.push((function(value) {
                    return function() {
                        console.log(value);
                    }
                }(i)));
            }

            funcs2.forEach(function(func) {
                func();
            });
            // let
            var funcs3 = [];
            for (let i = 0; i < 10; i++) {
                funcs3.push(function() {
                    console.log(i);
                });
            }
            funcs3.forEach(function(func) {
                func();
            })

            var funcs4 = [],
                obj = {
                    a: true,
                    b: false,
                    c: true
                };
            // 遍历对象 使用var 只能输出3次：c true
            for (const key in obj) {
                funcs4.push(function() {
                    console.log(key);
                    console.log(obj[key]);
                })
            }
            funcs4.forEach(function(func) {
                func();
            })
            text = '𠮷';
            for (let i = 0; i < 2; i++) {
                let point = text.codePointAt(i);
                // console.log(point);
                console.log(String.fromCodePoint(point));
            }
            console.log(text.length);
            console.log(String.fromCodePoint(134071));
            str = 'an apple a day keeps doctor away';
            console.log(str.includes('apple'));
            console.log(str.startsWith('apple', 3));
            console.log(str.endsWith('away'));
            console.log('呵呵呵呵 \n\呵呵呵呵 ')
            console.log(`呵呵呵呵\n呵呵呵呵`)
                // let message = tag`hhhhh`;
                // console.log(message)

            function testFunc(a, b = 1, c = 3) {
                console.log(a + b + c);
            }
            testFunc();
            testFunc(2);
            testFunc(1, 1, 1);

            function pick(object) {
                let result = Object.create(null);
                for (let i = 1, len = arguments.length; i < len; i++) {
                    result[arguments[i]] = object[arguments[i]];
                }
                return result;
            }
            person = {
                name: 'zhangsan',
                age: 12,
                gender: 'male'
            };
            let result = pick(person, 'age', 'gender');
            console.log(result.age, result.gender);
            let values = [1, 2, 3, 4, 5, 6];
            console.log(Math.max(8, ...values, 7))

            function Animal(name) {
                // if (this instanceof Animal) {
                //     this.name = name;
                // } 
                if (typeof new.target !== 'undefined') {
                    this.name = name;
                } else {
                    throw new Error('只允许用new来调用该函数')
                }
            }
            var animal = new Animal('猫');
            console.log(animal.name);
            // var notAAnimal = Animal('猫');
            // var notAAnimal = Animal.call(animal, '狗'); // 报错
            // console.log(notAAnimal);

            var jxd = {
                'first name': 'jiang',
                'last name': 'xiaodan'
            }
            console.log(jxd['first name']);
            let suffix = ' name';
            console.log(jxd['first' + suffix]);
            console.log(jxd['last' + suffix]);

            console.log(+0 == -0); // true
            console.log(-0 === +0); // true
            console.log(Object.is(+0, -0)); // false
            console.log(NaN == NaN); // false
            console.log(NaN === NaN); // false
            console.log(Object.is(NaN, NaN)); // true
            console.log(5 == '5'); // true
            console.log(5 === '5'); // false
            console.log(5 === 5); // true
            console.log(5 == 5); // true
            console.log(Object.is(5, '5')); // false
            console.log(Object.is(5, 5)); // true

            var oneObj = {
                name: 'oneObj',
                say() {
                    console.log(this.name + ' is saying');
                }
            }
            var twoObj = {
                name: 'twoObj',
                play() {
                    console.log(this.name + ' is playing');
                }
            }
            var newObj = {}
            Object.assign(newObj, oneObj, twoObj);
            newObj.name = 'newObj'
            newObj.say();
            newObj.play();

            var receiver = {}
            var supplier = {
                name: 'supplier',
                get name() {
                    return 'supplier';
                },
                set name(name) {
                    this.name = name;
                }
            }
            Object.assign(receiver, supplier);
            var srcDescriptor = Object.getOwnPropertyDescriptor(supplier, 'name');
            console.log(srcDescriptor.value);
            console.log(srcDescriptor.get);
            console.log(srcDescriptor.set);
            var descriptor = Object.getOwnPropertyDescriptor(receiver, 'name');
            console.log(descriptor.value);
            console.log(descriptor.get);
            console.log(descriptor.set); {
                let person = {
                    getGreeting() {
                        return 'hello';
                    }
                }

                let dog = {
                    getGreeting() {
                        return 'woof';
                    }
                }

                let friend = {
                    getGreeting() {
                        return super.getGreeting() + ', ...';
                    }
                }
                Object.setPrototypeOf(friend, person);
                console.log(friend.getGreeting());
                console.log(Object.getPrototypeOf(friend) === person);
                Object.setPrototypeOf(friend, dog);
                console.log(friend.getGreeting());
                console.log(Object.getPrototypeOf(friend) === dog);
            } {
                let person = {
                    getGreeting() {
                        return 'hello';
                    }
                };

                function shareGreeting() {
                    return 'Hi';
                }

                console.log(person.getGreeting().HomeObject);
            }
        }
    </script>
</head>

<body>
</body>

</html>